'libtgfx - Threaded Graphics library for FreeBASIC
'Copyright (C) 2013  Ebben Feagan <sir@mud.owlbox.net>
'
'This library is free software; you can redistribute it and/or
'modify it under the terms of the GNU Lesser General Public
'License as published by the Free Software Foundation; either
'version 2.1 of the License, or (at your option) any later version.
'
'This library is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
'Lesser General Public License for more details.
'
'You should have received a copy of the GNU Lesser General Public
'License along with this library; if not, write to the Free Software
'Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#include once "tgfx.bi"
#include once "tgfx-priv.bi"

namespace tgfx.image

    sub info(   byval src as any ptr, _
                byref width_ as integer, _
                byref height as integer, _
                byref pitch as integer _
            ) export

        var i = cast(fb.image ptr, src)
        width_ = i->width
        height = i->height
        pitch = i->pitch

    end sub

    function create(    byval x as integer, _
                        byval y as integer, _
                        byval defcolor as uinteger = &hffff00ff _
                    ) as any ptr export

        var cc = new CommChannel()
        var m1 = new MData(new line_data(0,tgfx.Coord(x,y),tgfx.Coord(0,0),defcolor,tgfx.false,tgfx.false),@null_free)
        Comm->send(new Message(_MCREATE,m1,cc))
        var m = cc->recv()
        while m = 0
            sleep 1,1
            m = cc->recv()
        wend
        delete cc
        var ret = m->x->d
        delete m
        return ret

    end function

    sub destroy( byval i as any ptr ) export
        imagedestroy i
    end sub

    function get(   byval src as any ptr = 0, _
                    byval f as Coord, _
                    byval s as Coord, _
                    byval dest as any ptr = 0 _
                ) as any ptr export
        dim ret as any ptr
        if dest = 0 then
            ret = create(abs(s.x-f.x),abs(s.y-f.y))
        else
            ret = dest
        end if
        var m1 = new MData(new line_data(ret,f,s,cast(uinteger,src),tgfx.false,tgfx.false),@null_free)
        var cc = new CommChannel()
        Comm->send(new Message(_MGET,m1,cc))
        var m = cc->recv()
        while m = 0 : sleep 1,1 : m = cc->recv() : wend
        delete m
        delete cc
        return ret
    end function

    sub put(    byval src as any ptr, _
                byval f as Coord, _
                byval dest as any ptr = 0 _
            ) export

        var m1 = new MData(new line_data(dest,f,tgfx.Coord(0,0),cast(uinteger,src),tgfx.false,tgfx.false),@null_free)
        var cc = new CommChannel()
        Comm->send(new Message(_MPUT,m1,cc))
        var m = cc->recv()
        while m = 0 : sleep 1,1 : m = cc->recv() : wend
        delete m
        delete cc
    end sub

end namespace
